<?php
/* @var $majors array */
/* @var $model1 array */
/* @var $model2 array */
/* @var $exts array */

/* @var $project_basic_info_id string */

use yii\bootstrap\Html;
use yii\bootstrap\Modal;
use yii\helpers\Url;
use yii\widgets\ActiveForm;

$this->registerCssFile('@web/js/datepicker/datepicker.css', ['depends' => ['backend\assets\AppAsset'], 'position' => $this::POS_HEAD]);
$this->registerJsFile('@web/js/datepicker/bootstrap.bundle.min.js', ['depends' => ['backend\assets\AppAsset'], 'position' => $this::POS_HEAD]);
$this->registerJsFile('@web/js/datepicker/datepicker.js', ['depends' => ['backend\assets\AppAsset'], 'position' => $this::POS_HEAD]);
$this->registerJsFile('@web/js/datepicker/datepicker.zh-CN.js', ['depends' => ['backend\assets\AppAsset'], 'position' => $this::POS_HEAD]);
//下拉
$this->registerCssFile('@web/js/select2/select2.css');
$this->registerJsFile('@web/js/select2/select2.full.min.js', ['depends' => ['backend\assets\AppAsset'], 'position' => $this::POS_HEAD]);

?>

<style>
    table td {
        text-align: center;
        vertical-align: middle;
        padding: 10px 10px 10px 10px;
    }

    .select2.select2-container {
        width: 100%;
        border: 1px solid #ccc;
        border-radius: 4px;
        padding: 2px 5px;
    }
</style>

<div class="row">
    <div class="col-sm-12">
        <div class="block-content" style="text-align:center">

            <?php $form = ActiveForm::begin([
                "options" => [
                    'id' => 'dataForm',
                    'action' => Url::to(['', 'project_basic_info_id' => $project_basic_info_id]),
                ],
                'enableAjaxValidation' => true,
                'validationUrl' => Url::toRoute(['validate211']),
            ]); ?>

            <table class="table-bordered " style="width: 100%;margin:30px 0px 0px 0px">
                <tr>
                    <td class=''>项目名称</td>
                    <td colspan="6" class="text-left">
                        <?= $form->field($model1, 'project_name')->textInput(['class' => 'form-control '])->label(false); ?>
                    </td>
                </tr>

                <tr>
                    <td class='col-sm-2'>项目定位</td>
                    <td colspan="1">主要面向产业</td>
                    <td class='col-sm-4' colspan="2">
                        <?= $form->field($model1, 'main_industry')->checkboxList([
                            1 => '新一代信息技术', 2 => "高端装备", 3 => "航空航天", 4 => "新能源汽车",
                            5 => "海洋装备", 6 => "新材料", 7 => "新能源", 8 => "生物技术",
                            9 => "绿色环保", 10 => "基础制造工艺", 11 => "基础零部件及元器件",
                        ], ['class' => 'form-control', 'style' => 'height: auto'])->label(false) ?>
                    </td>
                    <td colspan="1">
                        面向的职业岗位
                        <br>
                        <?php
                        echo Html::a('添加', NULL, [
                            'class' => "btn btn-primary btn-space add_post_group",
                            'style' => 'width:50px',
                        ])
                        ?>
                    </td>
                    <td colspan="2" class="col-sm-4 text-left post_group_td">
                        <?php
                        echo $form->field($model1, 'post_group')->hiddenInput(['class' => 'form-control '])->label(false);

                        if ($model1->post_group) {
                            $post_group_items = explode(",", $model1->post_group);
                            foreach ($post_group_items as $post_group_item) {
                                ?>
                                <div class="post_group_item">
                                    <span><?= $post_group_item ?></span>
                                    <input type="hidden" name="post_group[]" value="<?= $post_group_item ?>">
                                    <a class="btn btn-danger xs post_group_del">删除</a>
                                </div>
                            <?php }
                        } ?>

                    </td>
                </tr>
                <tr>
                    <td class=''>项目目标</td>
                    <td colspan="6" class="text-left">
                        <?= $form->field($model1, 'project_value')->textArea(['class' => 'form-control ', 'rows' => 4])->label(false); ?>
                    </td>
                </tr>
            </table>

            <table class="table-bordered border_top0" style="width: 100%;">
                <!--'supporting_majors' => '依托专业或专业群',
                -->
                <tr class="border_top0">
                    <td rowspan="15" class="col-sm-2 border_top0" id="rowspan_all">专业基础<br/>（专业基础包含专业，选择的专业是从“战略性新兴产业和产业基础能力重点领域”对应的专业中的选择）
                    </td>
                    <td class="border_top0">依托专业或专业群</td>
                    <td colspan="5" class="border_top0">
                        <?= $form->field($model2, 'supporting_majors')->textInput(['class' => 'form-control '])->label(false); ?>
                    </td>
                </tr>

                <tr>
                    <td colspan="6" class="text-right">
                        <?php echo Html::a('添加专业', NULL, ['class' => "btn btn-primary add-lines"]) ?>
                    </td>
                </tr>
                <tr>
                    <td class="" colspan="2">专业名称</td>
                    <td class="">开设时间</td>
                    <td class="">在校生数</td>
                    <td class="">2022届毕业生就业率(%)</td>
                    <td class="">操作</td>
                </tr>

                <!--'major_code' => '专业代码',
                'major_name' => '专业名称',
                'major_open_date' => '开设时间',
                'student_number' => '在校生数',
                'employment_rate' => '2022届毕业生就业率',-->
                <?php foreach ($exts as $k => $ext) { ?>
                    <tr class="major_tr">
                        <td colspan="2">
                            <div class="form-group" id="div_field2_<?= $k ?>">
                                <select name="tableName[<?= $k ?>][field2]" id="input_field2_<?= $k ?>"
                                        class="form-control input_field2">
                                    <option value="">请选择</option>
                                    <?php foreach ($majors as $major_k => $major_t) { ?>
                                        <option value="<?= $major_k ?>" <?= $major_t == $ext['major_name'] ? 'selected' : '' ?>>
                                            <?= $major_t ?>
                                        </option>
                                    <?php } ?>
                                </select>
                                <div class="help-block"></div>
                            </div>
                        </td>
                        <td>
                            <div class="form-group" id="div_field3_<?= $k ?>">
                                <input type="text" id="input_field3_<?= $k ?>" class="form-control input_field3"
                                       name="tableName[<?= $k ?>][field3]"
                                       value="<?= $ext['major_open_date'] ?>">
                                <div class="help-block"></div>
                            </div>
                        </td>
                        <td>
                            <div class="form-group" id="div_field4_<?= $k ?>">
                                <input type="text" id="input_field4_<?= $k ?>" class="form-control input_field4"
                                       name="tableName[<?= $k ?>][field4]"
                                       value="<?= $ext['student_number'] ?>">
                                <div class="help-block"></div>
                            </div>
                        </td>
                        <td>
                            <div class="form-group" id="div_field5_<?= $k ?>">
                                <input type="text" id="input_field5_<?= $k ?>" class="form-control input_field5"
                                       name="tableName[<?= $k ?>][field5]"
                                       value="<?= $ext['employment_rate'] ?>">
                                <div class="help-block"></div>
                            </div>
                        </td>
                        <td>
                            <?php
                            echo Html::a('删除', NULL, ['class' => 'btn btn-danger btn-space del-line']);
                            ?>
                        </td>
                    </tr>
                <?php } ?>
                <?php if ($model2->isNewRecord) { ?>
                    <tr class="major_tr">
                        <td colspan="2">
                            <div class="form-group" id="div_field2_0">
                                <select name="tableName[0][field2]" id="input_field2_0"
                                        class="form-control input_field2">
                                    <option value="">请选择</option>
                                    <?php foreach ($majors as $major_k => $major_t) { ?>
                                        <option value="<?= $major_k ?>"><?= $major_t ?></option>
                                    <?php } ?>
                                </select>
                                <div class="help-block"></div>
                            </div>
                        </td>

                        <td>
                            <div class="form-group" id="div_field3_0">
                                <input type="text" id="input_field3_0" class="form-control input_field3"
                                       name="tableName[0][field3]">
                                <div class="help-block"></div>
                            </div>
                        </td>
                        <td>
                            <div class="form-group" id="div_field4_0">
                                <input type="text" id="input_field4_0" class="form-control input_field4"
                                       name="tableName[0][field4]">
                                <div class="help-block"></div>
                            </div>
                        </td>
                        <td>
                            <div class="form-group" id="div_field5_0">
                                <input type="text" id="input_field5_0" class="form-control input_field5"
                                       name="tableName[0][field5]">
                                <div class="help-block"></div>
                            </div>
                        </td>
                        <td>
                            <a class="btn btn-danger btn-space del-line">删除</a></td>
                    </tr>
                <?php } ?>

                <!--
                'supporting_majors' => '依托专业或专业群',
                'course_instructor_number' => '专业课教师数',
                'number_one' => '副高及以上人数',
                'biteacher_number' => '双师型教师',
                'biteacher_ratio' => '双师型教师占比',

                -->
                <tr>
                    <td>专业课教师数(人)</td>
                    <td colspan="4">
                        <?= $form->field($model2, 'course_instructor_number')->textInput(['class' => 'form-control '])->label(false); ?>
                    </td>
                    <td>其中：</td>
                </tr>
                <tr>
                    <td>副高及以上人数(人)</td>
                    <td>
                        <?= $form->field($model2, 'number_one')->textInput(['class' => 'form-control '])->label(false); ?>
                    </td>
                    <td>双师型教师(人)</td>
                    <td colspan="">
                        <?= $form->field($model2, 'biteacher_number')->textInput(['class' => 'form-control '])->label(false); ?>
                    </td>
                    <td>双师型教师占比(%)</td>
                    <td colspan="2">
                        <?= $form->field($model2, 'biteacher_ratio')->textInput(['class' => 'form-control '])->label(false); ?>
                    </td>
                </tr>

            </table>

            <div class="form-group" style="text-align: center;margin-top: 20px; margin-bottom: 20px">
                <?php
                echo Html::submitButton('保存', ['class' => 'add btn btn-primary']);
                ?>

            </div>

            <?php ActiveForm::end(); ?>
        </div>

    </div>
</div>


<?php

$pageJs = <<<JS
  $.extend({
    nano:function(template,data){//模板
      return template.replace(/\{([\w\.]*)\}/g, function(str, key){
        var keys = key.split("."), value = data[keys.shift()];
        $.each(keys, function(){
          value = value[this];
        });
        return (value===null || value===undefined) ? "" : value;
      });
    }
  });
JS;
$this->registerJs($pageJs);

$Js = <<<JS
    var rowNum = $(".major_tr").length;
    //添加一行参与
    $(".add-lines").click(function () {
    var rowspan_all = $("#rowspan_all").attr('rowspan');
    $("#rowspan_all").attr("rowspan", Number(rowspan_all) + Number(1));

    rowNum++;
    $(".major_tr:last").after($.nano($("#major-templates").html(), {"num": rowNum}));
    //设置下拉
    $("select[id^='input_field2_']").select2({
        placeholder: "请选择",
        allowClear: false
    });
    // $('.select2').css('width','100%');

    console.log($.nano($("#major-templates").html(), {"num": rowNum}));

    //添加验证
    var id = "input_field1_" + rowNum;
    jQuery('#dataForm').yiiActiveForm("add", {
      "id": id,
      "container": "#div_field1_" + rowNum,
      "input": "#input_field1_" + rowNum,
      "validate": function (attribute, value, messages, deferred, form) {
        yii.validation.required(value, messages, {"message": "专业代码不能为空。"});
      }
    });
    jQuery('#dataForm').yiiActiveForm("add", {
      "id": "input_field2_" + rowNum,
      "container": "#div_field2_" + rowNum,
      "input": "#input_field2_" + rowNum,
      "validate": function (attribute, value, messages, deferred, form) {
        yii.validation.required(value, messages, {"message": "专业名称不能为空。"});
      }
    });
    jQuery('#dataForm').yiiActiveForm("add", {
      "id": "input_field3_" + rowNum,
      "container": "#div_field3_" + rowNum,
      "input": "#input_field3_" + rowNum,
      "validate": function (attribute, value, messages, deferred, form) {
        yii.validation.required(value, messages, {"message": "开设时间不能为空。"});
      }
    });
    
    $('#input_field3_' + rowNum).datepicker({
      todayHighlight: true,
      autoclose: true,
      language: "zh-CN",
      minViewMode: 0,
      format: "yyyy-mm-dd",
      autoHide: true,
    });
    
    jQuery('#dataForm').yiiActiveForm("add", {
      "id": "input_field4_" + rowNum,
      "container": "#div_field4_" + rowNum,
      "input": "#input_field4_" + rowNum,
      "validate": function (attribute, value, messages, deferred, form) {
        yii.validation.required(value, messages, {"message": "在校生数不能为空。"});
        yii.validation.number(value, messages, {
          "pattern": /^[0-9]\d*$/,
          "message": "在校生数必须是整数。",
          "max": 2147483647,
          "tooBig": "值必须不大于2147483647。",
          "skipOnEmpty": 1
        });
      }
    });
    jQuery('#dataForm').yiiActiveForm("add", {
      "id": "input_field5_" + rowNum,
      "container": "#div_field5_" + rowNum,
      "input": "#input_field5_" + rowNum,
      "validate": function (attribute, value, messages, deferred, form) {
        yii.validation.required(value, messages, {"message": "2022届毕业生就业率不能为空。"});
        yii.validation.number(value, messages, {
          "pattern": /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/,
          "message": "必须是整数或保留两位小数",
          "skipOnEmpty": 1
        });
      }
    });
  });

  //删除一行牵头
  $("#dataForm").on("click", ".del-line", function () {
    if(confirm('确认删除吗')) {
        var left_rowNum = $(".major_tr").length;
        if (left_rowNum < 2) {
          myAlert('专业不能少于1');
          return false;
        }

        var rowspan_all = $("#rowspan_all").attr('rowspan');
        $("#rowspan_all").attr("rowspan", Number(rowspan_all) - Number(1));

        $(this).closest('tr').remove();
    }

  });

  $(".input_field1").each(function (i, ele) {
    var id = $(this).attr("id");
    jQuery('#dataForm').yiiActiveForm("add", {
      "id": id,
      "container": "#div_field1_" + i,
      "input": "#input_field1_" + i,
      "validate": function (attribute, value, messages, deferred, form) {
        yii.validation.required(value, messages, {"message": "专业代码不能为空。"});
      }
    });
  });
  $(".input_field2").each(function (i, ele) {
    var id = $(this).attr("id");
    jQuery('#dataForm').yiiActiveForm("add", {
      "id": id,
      "container": "#div_field2_" + i,
      "input": "#input_field2_" + i,
      "validate": function (attribute, value, messages, deferred, form) {
        yii.validation.required(value, messages, {"message": "专业名称不能为空。"});
      }
    });
  });
  
  $(".input_field3").each(function (i, ele) {
    var id = $(this).attr("id");
    jQuery('#dataForm').yiiActiveForm("add", {
      "id": id,
      "container": "#div_field3_" + i,
      "input": "#input_field3_" + i,
      "validate": function (attribute, value, messages, deferred, form) {
        yii.validation.required(value, messages, {"message": "开设时间不能为空。"});
      }
    });
    
    $("#input_field3_" + i ).datepicker({
      todayHighlight: true,
      autoclose: true,
      language: "zh-CN",
      minViewMode: 0,
      format: "yyyy-mm-dd",
      autoHide: true,
    });
  });
  
  $(".input_field4").each(function (i, ele) {
    var id = $(this).attr("id");
    jQuery('#dataForm').yiiActiveForm("add", {
      "id": id,
      "container": "#div_field4_" + i,
      "input": "#input_field4_" + i,
      "validate": function (attribute, value, messages, deferred, form) {
        yii.validation.required(value, messages, {"message": "在校生数不能为空。"});
        yii.validation.number(value, messages, {
          "pattern": /^[0-9]\d*$/,
          "message": "在校生数必须是正整数。",
          "max": 2147483647,
          "tooBig": "值必须不大于2147483647。",
          "skipOnEmpty": 1
        });
      }
    });
  });
  $(".input_field5").each(function (i, ele) {
    var id = $(this).attr("id");
    jQuery('#dataForm').yiiActiveForm("add", {
      "id": id,
      "container": "#div_field5_" + i,
      "input": "#input_field5_" + i,
      "validate": function (attribute, value, messages, deferred, form) {
        yii.validation.required(value, messages, {"message": "2022届毕业生就业率不能为空。"});
        yii.validation.number(value, messages, {
          "pattern": /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2}))|0)$/,
          "message": "必须是整数或保留两位小数",
          "skipOnEmpty": 1
        });
      }
    });
  });
  
JS;
$this->registerJs($Js);
?>

<script type="text/template" id="major-templates">
    <tr class="major_tr">
        <td colspan="2">
            <div class="form-group" id="div_field2_{num}">
                <select name="tableName[{num}][field2]" id="input_field2_{num}" class="form-control input_field2">
                    <option value="">请选择</option>
                    <?php foreach ($majors as $major_k => $major_t) { ?>
                        <option value="<?= $major_k ?>"><?= $major_t ?></option>
                    <?php } ?>
                </select>
                <div class="help-block"></div>
            </div>
        </td>

        <td>
            <div class="form-group" id="div_field3_{num}">
                <input type="text" id="input_field3_{num}" class="form-control input_field3"
                       name="tableName[{num}][field3]">
                <div class="help-block"></div>
            </div>
        </td>
        <td>
            <div class="form-group" id="div_field4_{num}">
                <input type="text" id="input_field4_{num}" class="form-control input_field4"
                       name="tableName[{num}][field4]">
                <div class="help-block"></div>
            </div>
        </td>
        <td>
            <div class="form-group" id="div_field5_{num}">
                <input type="text" id="input_field5_{num}" class="form-control input_field5"
                       name="tableName[{num}][field5]">
                <div class="help-block"></div>
            </div>
        </td>
        <td>
            <?php
            echo Html::a('删除', NULL, [
                'class' => "btn btn-danger btn-space del-line",
            ]);
            ?>
        </td>
    </tr>
</script>

<script type="text/template" id="post-input-templates">
    <div class="form-group">
        <textarea id="post_group_input" class="form-control height400" rows="3"></textarea>
    </div>
    <div class="form-group" style="margin-top: 30px">
        <a class="btn btn-primary add_post_group_save">添加</a>
    </div>
</script>
<style>
    .post_group_item {
        margin: 10px 0;
    }

    .post_group_del {
        margin-left: 10px;
    }
</style>

<script type="text/template" id="post-templates">
    <div class="post_group_item">
        <span>{item_value}</span>
        <input type="hidden" name="post_group[]" value="{item_value}">
        <a class="btn btn-danger xs post_group_del">删除</a>
    </div>
</script>


<?php


Modal::begin([
    'id' => 'modal66',
    'header' => '<h4 class="modal-title" ></h4>',
    'size' => 'modal-mid',
    'options' => [
        'data-backdrop' => 'static',//点击空白处不关闭弹窗
        'data-keyboard' => false,
    ],
]);
Modal::end();

$Js = <<<JS
    $("select[id^='input_field2_']").select2({
        placeholder: "请选择",
        allowClear: false
    });
    $('.select2').css('width', '100%');

    $(".add_post_group").click(function () {
        var left_rowNum = $(".post_group_item").length;
        if (left_rowNum >= 3) {
            myAlert('职业岗位最多只能添加三个');
            return false;
        }
        $("#modal66 .modal-title").html('添加职业岗位名称');
        $("#modal66 .modal-body").html($("#post-input-templates").html());
        $("#modal66").modal('show');
    });

    $(document).on("click", ".add_post_group_save", function () {
        var item_value = $("#post_group_input").val();
        console.log('item_value', item_value);
        if (!item_value) {
            myAlert('职业岗位还没填写');
            return false;
        }
        var html = $.nano($("#post-templates").html(), {"item_value": item_value});
        $(".post_group_td").append(html);

        $("#formprojectbasicinfo-post_group").val(item_value);
        $(".field-formprojectbasicinfo-post_group").find('.help-block').html('');
        $("#post_group_input").val('');
        $("#modal66").modal('hide');

    });

    //删除一行牵头
    $(document).on("click", ".post_group_del", function () {
        var left_rowNum = $(".post_group_item").length;
        if (left_rowNum < 2) {
            myAlert('职业岗位不能少于1个');
            return false;
        }

        $(this).closest('.post_group_item').remove();

    });
JS;
$this->registerJs($Js);
?>
<script>


</script>


